package com.apes.crm.car.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CarBase.CONST_TABLE_NAME, indexes = {
        @Index(name = "IDX_CRM_CAR_BASE_ORIGIN", columnList = "origin", unique = true),
        @Index(name = "INX_CRM_CAR_FRAME_NUMBER", columnList = "frame_number")
})
public class CarBase extends Model {
    public static final String CONST_TABLE_NAME = "crm_car_base";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车辆基本信息", padding = 8)
    private String id; //车辆唯一识别码

    @Column(name = "frame_number", length = 64)
    private String frameNumber; //车架号码

    @Column(name = "engine_number", length = 64)
    private String engineNumber; //发动机号码

    @Column(name = "body_color")
    private String bodyColor;    //车身颜色

    @Column(name = "color_code", length = 16)
    private String colorCode;    //色号代码

    @Column(name = "config_model", length = 64)
    private String configModel;  //配置型号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "paint_type",referencedColumnName = "id")
    private Enumeration paintType;    //车漆类型

    @Column(name="new_car_price", nullable = false, columnDefinition = "number(16,4) default 0")
    private double newCarPrice; //新车购置价（万元）

    @Column(name="body_front_photo")
    private String bodyFrontPhoto; //车身正面照片

    @Column(name="body_reverse_photo")
    private String bodyReversePhoto; // 车身反面照片

    @Column(name="nameplate_photo")
    private String nameplatePhoto; // 铭牌照片

    @Column(name="jy_id", length = 64)
    private String jyId; // 精友车型编码

    @Column(name="jy_name")
    private String jyName; // 精友车型名称

    @Column(name="modify_record")
    private String modifyRecord; // 改装记录

    @Column(name="origin")
    private String origin; // yyerp CLNM

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name="forced_retirement_date")
    private Date forcedRetirementDate; // 强制报废时间

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_configuration_id",referencedColumnName = "id")
    private CarConfiguration carConfiguration; // 配置车型编码  从车辆主数据取

    @OneToOne(mappedBy = "car", cascade = CascadeType.ALL, orphanRemoval = true)
    private CarBusiness carBusiness;  //汽车商务信息

    public CarBusiness getCarBusiness() {
        if(carBusiness != null) {
            carBusiness.setCar(this);
        }
        return carBusiness;
    }

    @OneToOne(mappedBy = "car", cascade = CascadeType.ALL, orphanRemoval = true)
    private CarMaintenance carMaintenance;  //汽车保养信息

    public CarMaintenance getCarMaintenance() {
        if(carMaintenance != null) {
            carMaintenance.setCar(this);
        }
        return  carMaintenance;
    }


    /***
     * 功能：查询车型
     * @return
     */
    public CarModel queryCarModel() {
        if (carConfiguration == null) return null;
        return carConfiguration.getCarModel();
    }

    /***
     * 功能：查询厂牌
     * @return
     */
    public CarLabel queryCarLabel() {
        if (carConfiguration == null) return null;
        return carConfiguration.getCarLabel();
    }

}
